package com.easy.easylibdemo.ui.greendao;

import android.os.Bundle;

import com.easy.easylibdemo.R;
import com.easy.easylibdemo.gen.DaoSession;
import com.easy.easylibdemo.gen.FourBeanDao;
import com.easy.easylibdemo.gen.OneBeanDao;
import com.easy.easylibdemo.gen.ThreeBeanDao;
import com.easy.easylibdemo.gen.TwoBeanDao;
import com.easy.easylibdemo.ui.greendao.model.FourBean;
import com.easy.easylibdemo.ui.greendao.model.OneBean;
import com.easy.easylibdemo.ui.greendao.model.ThreeBean;
import com.easy.easylibdemo.ui.greendao.model.TwoBean;
import com.easy.easylibdemo.ui.greendao.utils.GreenDaoUtils;
import com.easylib.base.BaseActivity;
import com.easylib.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class GreenActivity extends BaseActivity {

    @Override
    public int getContentViewId() {
        return R.layout.activity_green;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initVariable() {
        twoDao=GreenDaoUtils.getInstance().getDaoSession().getTwoBeanDao();
        oneDao=GreenDaoUtils.getInstance().getDaoSession().getOneBeanDao();
        threeDao=GreenDaoUtils.getInstance().getDaoSession().getThreeBeanDao();
        fourDao=GreenDaoUtils.getInstance().getDaoSession().getFourBeanDao();
    }
    TwoBeanDao twoDao;
    OneBeanDao oneDao;
    ThreeBeanDao threeDao;
    FourBeanDao fourDao;
    List<TwoBean> list;
    List<ThreeBean> list3;
    @Override
    protected void processLogic(Bundle savedInstanceState) {
//        oneDao.deleteAll();
//        twoDao.deleteAll();
        if (oneDao.count()==0){
            LogUtils.e(TAG,"===start 插入数据===");
            setDatas();
            LogUtils.e(TAG,"===end 插入数据===");
        }

        threeDao.insert(new ThreeBean(null,"three","lastname"));

        list=twoDao.loadAll();
        for (int i = 0; i < list.size(); i++) {
            LogUtils.e(TAG,"==="+list.get(i).getTwoName()+"===");
        }
        list3=threeDao.loadAll();
        for (int i = 0; i < list3.size(); i++) {
            LogUtils.e(TAG,"==="+list3.get(i).getName()+"===");
        }



    }

    private void setDatas() {
        OneBean oneBean1=new OneBean(1L);
        OneBean oneBean2=new OneBean(2L);
        TwoBean twoBean;

        LogUtils.e(TAG,"===插入Two表 10个twoBeanId=1的数据===");
        for (int i = 0; i < 10; i++) {
            twoBean=new TwoBean(null,1L,"name"+i);
            twoDao.insert(twoBean);
        }
        LogUtils.e(TAG,"===插入Two表 5个twoBeanId=2的数据===");
        for (int i = 0; i < 5; i++) {
            twoBean=new TwoBean(null,2L,"name"+i);
            twoDao.insert(twoBean);
        }
        LogUtils.e(TAG,"===插入ONE表 Id=1的数据===");
        oneDao.insertOrReplace(oneBean1);
        LogUtils.e(TAG,"===插入ONE表 Id=2的数据===");
        oneDao.insertOrReplace(oneBean2);
        LogUtils.e(TAG,"===删除Two表 5个twoBeanId=1 name5的数据===");
        twoBean=new TwoBean(null,1L,"name"+4 );
        twoDao.delete(twoBean);
        LogUtils.e(TAG,"===插入Four表 name=four的数据===");
        fourDao.insert(new FourBean(null,"four"));
    }
}
